package com.hanthink.web.controller.common;

import java.io.IOException;
import java.util.List;

import io.minio.StatObjectResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hanthink.common.config.HdfsProperties;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.file.MinioUtils;

import io.minio.messages.Bucket;

/**
 * <p>
 * Title: MinioOSSController
 * </p>
 * <p>
 * Description: Minio OSS对象存储
 * </p>
 *
 * @author vondser
 * @date 2020年12月9日
 */
@RestController
@RequestMapping("/minio/oss")
public class MinioOSSController {

	protected final Logger logger = LoggerFactory.getLogger(MinioOSSController.class);

	@Autowired
	private MinioUtils minioUtils;

	@Autowired
	private HdfsProperties hdfsProperties;

	/**
	 * PC文件上传
	 */
	@PostMapping("/putObject")
	public AjaxResult pubObj(@RequestParam("file") MultipartFile file) throws IOException {
		if (!file.isEmpty()) {
			try {
				// pc端上传固定上传到基础数据文件桶
//				minioUtils.getCurrentBucket();
				String bucketName = "file-base";
				String fileUrl = minioUtils.uploadFile(file,bucketName);
				AjaxResult ajax = AjaxResult.success();
				ajax.put("fileUrl", fileUrl);
				logger.info(fileUrl);
				return ajax;
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		return AjaxResult.error("上传文件异常，请联系管理员");
	}

	/**
	 * 文件上传
	 */
	@PostMapping("/appPutObject")
	public AjaxResult appPubObj(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName)
			throws IOException {
		if (!file.isEmpty()) {
			try {
				// 文件桶判断
				minioUtils.getCurrentBucket();
				String fileUrl = minioUtils.appUploadFile(file, fileName);
				AjaxResult ajax = AjaxResult.success();
				ajax.put("fileUrl", fileUrl);
				logger.info(fileUrl);
				return ajax;
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		return AjaxResult.error("上传文件异常，请联系管理员");
	}

	/**
	 * <p>
	 * Title: getAllBuckets11
	 * </p>
	 * <p>
	 * Description: 获取Bucket列表
	 * </p>
	 *
	 * @return
	 */
	@GetMapping("/getAllBuckets")
	public AjaxResult getAllBuckets() {
		AjaxResult ajax = AjaxResult.success();
		try {
			List<Bucket> allBuckets = minioUtils.getAllBuckets();
			ajax.put("bucketList", allBuckets);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return ajax;
	}

	/**
	 * <p>
	 * Title: getFileInfo
	 * </p>
	 * <p>
	 * Description: 获取文件信息
	 * </p>
	 *
	 * @return
	 */
	@GetMapping("/{fname}")
	public AjaxResult getFileInfo(@PathVariable String fname) {
		AjaxResult ajax = AjaxResult.success();
		try {
			StatObjectResponse objectInfo = minioUtils.getObjectInfo(fname);
//			String objectURL = minioUtils.getObjectURL(fname);
//			ajax.put("bucketName", objectInfo.bucket());
//			ajax.put("contentType", objectInfo.contentType());
//			ajax.put("createdTime", objectInfo.lastModified());
//			ajax.put("name", objectInfo.contentType());
//			ajax.put("length", objectInfo.size());
//			ajax.put("etag", objectInfo.etag());
//			if (objectURL != null && !"".equals(objectURL)) {
			String msg = "http://39.100.236.78:9000/" + hdfsProperties.getBucketName() + "/" + fname;
			ajax.put("objectURL", msg);
//			} else {
//				ajax.put("objectURL", objectURL);
//			}
//			ajax.put("objectURL", objectURL);
		} catch (Exception e) {

			logger.error(e.getMessage());
			throw new CustomException("未获取到文件信息");
		}
		return ajax;
	}

	/**
	 * <p>
	 * Title: getFileInfo
	 * </p>
	 * <p>
	 * Description: 获取文件信息
	 * </p>
	 *
	 * @return
	 */
	@GetMapping("/getFileName")
	public AjaxResult getFileName(@RequestParam("fileType") String fileType) {
		AjaxResult ajax = AjaxResult.success();
		try {
			// 文件桶判断
			minioUtils.getCurrentBucket();
			String resFilelName = minioUtils.createFileName(fileType);
			ajax.put("fileName", resFilelName);
		} catch (Exception e) {

			logger.error(e.getMessage());
			throw new CustomException("未获取到文件信息");
		}
		return ajax;
	}

	/**
	 * <p>
	 * Title: remove
	 * </p>
	 * <p>
	 * Description: 删除文件
	 * </p>
	 *
	 * @param roleIds
	 * @return
	 */
	@DeleteMapping("/{objName}")
	public AjaxResult remove(@PathVariable String objName) {
		AjaxResult ajax = AjaxResult.success();
		try {
			// 文件桶判断
			minioUtils.getCurrentBucket();
			minioUtils.removeObject(objName);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return ajax;
	}

}
